package com.atguigu.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Properties;

/**
 * TODO 消费者组
 * 消费者不需要关闭资源，因为消费者是一个永动机，要不断的拉取消息。
 */
public class ConsumerClientGroupClient2 {
    public static void main(String[] args) {
        //1.声明配置文件
        Properties properties = new Properties();
        //2.指定broker地址
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092,hadoop103:9092");
        //3.对消息的key和value进行反序列化
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        //3.1指定当前消费者对应的组id
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"group02");
        //4.获取客户端连接对象
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(properties);
        //5.订阅主题
        ArrayList<String> topics = new ArrayList<>();
        topics.add("bigdata");
        kafkaConsumer.subscribe(topics);
        //6.拉取对应消息
        while (true){
            ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofSeconds(1));
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record);
            }
        }
    }
}
